package com.fyx.controller;

import com.alibaba.fastjson.JSONObject;
import com.fyx.annotation.Log;
import com.fyx.entity.*;
import com.fyx.modules.system.service.DataService;
import com.fyx.modules.system.service.DeptService;
import com.fyx.modules.system.service.RoleService;
import com.fyx.modules.system.service.UserService;
import com.fyx.modules.system.service.dto.RoleSmallDto;
import com.fyx.modules.system.service.dto.UserDto;
import com.fyx.service.IChannelService;
import com.fyx.until.DateUtil;
import com.fyx.utils.PageUtil;
import com.fyx.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Api(tags = "渠道推广")
@RestController
@RequestMapping("/api/channel")
@RequiredArgsConstructor
public class ChannelController {
    private final DeptService deptService;
    private final UserService userService;
    private final DataService dataService;
    private final RoleService roleService;
    private final IChannelService iChannelService;

    @Autowired
    private IChannelService IChannelService;
    @Log("查询渠道用户统计")
    @ApiOperation("查询渠道用户统计")
    @GetMapping(value ="channelUser")
    @PreAuthorize("@el.check('channelUser:list')")
    public ResponseEntity<Object> query(UserEntity userEntity, Pageable pageable){
        // 数据权限

        if (!ObjectUtils.isEmpty(userEntity.getDeptId())) {
            userEntity.getDeptIds().add(userEntity.getDeptId());
            userEntity.getDeptIds().addAll(deptService.getDeptChildren(deptService.findByPid(userEntity.getDeptId())));
        }
        List<Long> dataScopes = dataService.getDeptIds(userService.findByName(SecurityUtils.getCurrentUsername()));
        if (!CollectionUtils.isEmpty(userEntity.getDeptIds()) && !CollectionUtils.isEmpty(dataScopes)){
            // 取交集
            userEntity.getDeptIds().retainAll(dataScopes);
        } else {
            // 否则取并集
            userEntity.getDeptIds().addAll(dataScopes);
        }
        Map mapResult = IChannelService.channelUser(userEntity,pageable);
        return new ResponseEntity<>(PageUtil.toPage(mapResult.get("list"),mapResult.get("total")),HttpStatus.OK);
    }

    @Log("查询渠道充值记录")
    @ApiOperation("查询渠道充值记录")
    @GetMapping(value ="channelRecharge")
    @PreAuthorize("@el.check('channelRecharge:list')")
    public ResponseEntity<Object> channelRecharge(OrderEntity orderEntity, Pageable pageable){
        // 数据权限
        if (!ObjectUtils.isEmpty(orderEntity.getDeptId())) {
            orderEntity.getDeptIds().add(orderEntity.getDeptId());
            orderEntity.getDeptIds().addAll(deptService.getDeptChildren(deptService.findByPid(orderEntity.getDeptId())));
        }
        List<Long> dataScopes = dataService.getDeptIds(userService.findByName(SecurityUtils.getCurrentUsername()));
        if (!CollectionUtils.isEmpty(orderEntity.getDeptIds()) && !CollectionUtils.isEmpty(dataScopes)){
            // 取交集
            orderEntity.getDeptIds().retainAll(dataScopes);
        } else {
            // 否则取并集
            orderEntity.getDeptIds().addAll(dataScopes);
        }
        Map mapResult = IChannelService.channelRecharge(orderEntity,pageable);
        return new ResponseEntity<>(PageUtil.toPage(mapResult.get("list"),mapResult.get("total")), HttpStatus.OK);
    }

    @Log("查询渠道充值统计")
    @ApiOperation("查询渠道充值统计")
    @GetMapping(value = "getChannelRechargeCountList")
    @PreAuthorize("@el.check('channelRechargeCount:list')")
    public Map getChannelRechargeCountList(ChannelDataEntity channelDataEntity, Pageable pageable){
        // 数据权限
        if (!ObjectUtils.isEmpty(channelDataEntity.getDeptId())) {
            channelDataEntity.getDeptIds().add(channelDataEntity.getDeptId());
            channelDataEntity.getDeptIds().addAll(deptService.getDeptChildren(deptService.findByPid(channelDataEntity.getDeptId())));
        }

        List<Long> dataScopes = dataService.getDeptIds(userService.findByName(SecurityUtils.getCurrentUsername()));
        if (!CollectionUtils.isEmpty(channelDataEntity.getDeptIds()) && !CollectionUtils.isEmpty(dataScopes)){
            // 取交集
            channelDataEntity.getDeptIds().retainAll(dataScopes);
        } else {
            // 否则取并集
            channelDataEntity.getDeptIds().addAll(dataScopes);
        }
        Map mapResult = IChannelService.channelRechargeCount(channelDataEntity,pageable);
        return mapResult;
    }
    @Log("查询渠道充值统计")
    @ApiOperation("查询渠道充值统计")
    @PostMapping(value = "selectChannelDataTree")
    @PreAuthorize("@el.check('channelRechargeCount:list')")
    public List<ChannelData> selectChannelDataTree(@Validated @RequestBody JSONObject jsonObject){
        String channel= jsonObject.getString("channel");
        Integer time=Integer.valueOf(jsonObject.getString("time")) ;
        List<ChannelData> getChannelDataList = IChannelService.selectChannelDataTree(time,channel);
        return getChannelDataList;
    }


    @Log("查询渠道充值统计")
    @ApiOperation("查询渠道充值统计")
    @GetMapping(value = "getLastMonthData")
    @PreAuthorize("@el.check('channelRechargeCount:list')")
    public Map getLastMonthData(ChannelDataEntity channelDataEntity, Pageable pageable){
        List<Timestamp> qTime = new ArrayList<>();
        Timestamp timestamp0 =(Timestamp) DateUtil.getBeginDayOfLastMonth();
        Timestamp timestamp1 =(Timestamp) DateUtil.getEndDayOfLastMonth();
        qTime.add(timestamp0);
        qTime.add(timestamp1);
        channelDataEntity.setQTime(qTime);
        // 数据权限
        if (!ObjectUtils.isEmpty(channelDataEntity.getDeptId())) {
            channelDataEntity.getDeptIds().add(channelDataEntity.getDeptId());
            channelDataEntity.getDeptIds().addAll(deptService.getDeptChildren(deptService.findByPid(channelDataEntity.getDeptId())));
        }
        List<Long> dataScopes = dataService.getDeptIds(userService.findByName(SecurityUtils.getCurrentUsername()));
        if (!CollectionUtils.isEmpty(channelDataEntity.getDeptIds()) && !CollectionUtils.isEmpty(dataScopes)){
            // 取交集
            channelDataEntity.getDeptIds().retainAll(dataScopes);
        } else {
            // 否则取并集
            channelDataEntity.getDeptIds().addAll(dataScopes);
        }
        Map mapResult = IChannelService.channelRechargeCount(channelDataEntity,pageable);
        return mapResult;
    }

    @Log("查询渠道充值统计")
    @ApiOperation("查询渠道充值统计")
    @GetMapping(value = "getCurrentMonthData")
//    @PreAuthorize("@el.check('channelRechargeCount:list')")
    public Map getCurrentMonthData(ChannelDataEntity channelDataEntity, Pageable pageable){
        List<Timestamp> qTime = new ArrayList<>();
        Timestamp timestamp0 =(Timestamp) DateUtil.getBeginDayOfMonth();
        Timestamp timestamp1 =(Timestamp) DateUtil.getEndDayOfMonth();
        qTime.add(timestamp0);
        qTime.add(timestamp1);
        channelDataEntity.setQTime(qTime);
        // 数据权限
        if (!ObjectUtils.isEmpty(channelDataEntity.getDeptId())) {
            channelDataEntity.getDeptIds().add(channelDataEntity.getDeptId());
            channelDataEntity.getDeptIds().addAll(deptService.getDeptChildren(deptService.findByPid(channelDataEntity.getDeptId())));
        }
        List<Long> dataScopes = dataService.getDeptIds(userService.findByName(SecurityUtils.getCurrentUsername()));
        if (!CollectionUtils.isEmpty(channelDataEntity.getDeptIds()) && !CollectionUtils.isEmpty(dataScopes)){
            // 取交集
            channelDataEntity.getDeptIds().retainAll(dataScopes);
        } else {
            // 否则取并集
            channelDataEntity.getDeptIds().addAll(dataScopes);
        }
        Map mapResult = IChannelService.channelRechargeCount(channelDataEntity,pageable);
        return mapResult;
    }



    @Log("查询渠道ltv")
    @ApiOperation("查询渠道ltv")
    @GetMapping(value="channelLtv")
    @PreAuthorize("@el.check('channelLtv:list')")
    public ResponseEntity<Object> channelLtv(ChannelLtv channelLtv, Pageable pageable){
        // 数据权限
        if (!ObjectUtils.isEmpty(channelLtv.getDeptId())) {
            channelLtv.getDeptIds().add(channelLtv.getDeptId());
            channelLtv.getDeptIds().addAll(deptService.getDeptChildren(deptService.findByPid(channelLtv.getDeptId())));
        }
        List<Long> dataScopes = dataService.getDeptIds(userService.findByName(SecurityUtils.getCurrentUsername()));
        if (!CollectionUtils.isEmpty(channelLtv.getDeptIds()) && !CollectionUtils.isEmpty(dataScopes)){
            // 取交集
            channelLtv.getDeptIds().retainAll(dataScopes);
        } else {
            // 否则取并集
            channelLtv.getDeptIds().addAll(dataScopes);
      }

        Map mapResult = IChannelService.channelLtv(channelLtv,pageable);
        return new ResponseEntity<>(PageUtil.toPage(mapResult.get("list"),mapResult.get("total")), HttpStatus.OK);
    }

    @Log("ios渠道数据")
    @ApiOperation("ios渠道数据")
    @GetMapping(value="ioschannel")
    @PreAuthorize("@el.check('ioschannel:list')")
    public Map ioschannel(IosCdEntity iosCdEntity, Pageable pageable){
        Map mapResult = IChannelService.ioschannel(iosCdEntity,pageable);
        return mapResult;
    }


    @Log("付费留存统计")
    @ApiOperation("付费留存统计")
    @GetMapping(value="channelId")
    @PreAuthorize("@el.check('channelId:list')")
    public Map channelId(ChannelIdData channelIdData, Pageable pageable){
        Map mapResult = iChannelService.channelIdData(channelIdData,pageable);
        return mapResult;
    }
}
